//
//  NewsViewController.h
//  WeLoveNews
//
//  Created by dllo on 15/10/7.
//  Copyright (c) 2015年 杜怡方. All rights reserved.
//

#import "SliderButton.h"

#define Kheight [[UIScreen mainScreen]bounds].size.height / 667

// 按钮的宽高
#define kButtonHeight 23 * ([[UIScreen mainScreen]bounds].size.height / 667)
#define kButtonWidth  ([[UIScreen mainScreen] bounds].size.width - 60) / 2
// 按钮间距
#define kSpace 40 * Kheight


#define kBtnTitbleColor [UIColor blackColor]
// 按钮未选中时颜色
#define kBtnTitleUnselectedColor [UIColor colorWithRed:0.7361 green:0.7361 blue:0.7361 alpha:1.0]

@interface SliderButton ()

/**
 *  按钮的标识条
 */
@property (nonatomic, strong) UIView *lineView;
@end

@implementation SliderButton


- (instancetype)initWithFrame:(CGRect)frame andBtnTitleArray:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.btnTitleArray = [NSArray arrayWithArray:array];
    }
    return self;
}
// 当数组传进来值的时候, 创建视图
- (void)setBtnTitleArray:(NSArray *)btnTitleArray
{
    if (_btnTitleArray != btnTitleArray) {
        _btnTitleArray = btnTitleArray ;
    }
    
    UIScrollView *btnScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 2 * Kheight)];
    // 设置 btnScrollView 的contentSize
    btnScrollView.contentSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width, 0);
    btnScrollView.scrollsToTop = NO;
    btnScrollView.backgroundColor = [UIColor clearColor];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
    effectView.frame = self.frame;
    [btnScrollView addSubview:effectView];
    //[effectView release];
    for (int i = 0; i < _btnTitleArray.count; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10 * Kheight + (kButtonWidth + kSpace) * i, 10 * Kheight, kButtonWidth, kButtonHeight);
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:_btnTitleArray[i] forState:UIControlStateNormal];
        
        if (i == 0) {
            [btn setTitleColor:kBtnTitbleColor forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
        }
        else{
            [btn setTitleColor:kBtnTitleUnselectedColor forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
        }
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i + 100;
        btnScrollView.tag = 99;
        [btnScrollView addSubview:btn];
    }
    btnScrollView.showsHorizontalScrollIndicator = NO;
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(10 * Kheight, 10 * Kheight + kButtonHeight + 2 * Kheight, kButtonWidth, 2 * Kheight)];
    self.lineView.backgroundColor = [UIColor colorWithRed:0.945 green:0.914 blue:0.129 alpha:1.000];
    [btnScrollView addSubview:self.lineView];
    [self addSubview:btnScrollView];
}
// 按钮点击事件
- (void)btnAction:(UIButton *)sender
{
    // 控制按钮的选种颜色
    for (UIButton *btn in [self viewWithTag:99].subviews) {

        if ([btn isKindOfClass:[UIButton class]]) {
            if (btn.tag == sender.tag) {
                [btn setTitleColor:kBtnTitbleColor forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:16];
            }else
            {
                [btn setTitleColor:kBtnTitleUnselectedColor forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:14];
            }
        }
    }
    
    [UIView animateWithDuration:0.3 animations:^{
       
        self.lineView.frame = CGRectMake(sender.frame.origin.x, self.lineView.frame.origin.y, kButtonWidth, 2 * Kheight);
        
    }];
    
    // 执行代理方法, 返回当前选中的按钮
    [self.delegate buttonOfSliderButtonClicked:sender];
}

- (void)setButtonTag:(NSInteger)buttonTag
{
    UIButton *button = (UIButton *)[self viewWithTag:buttonTag];
    [self btnAction:button];
}
@end
